'use strict';

require('../../assets/scss/entry/common.scss');
require('../../assets/scss/entry/agency.scss');
require('../../assets/scss/entry/login.scss');

let $ = jQuery = window.jQuery = window.$ = require('jquery');
//时间转换插件
let moment = require('moment');
require('../../libs/jquery.cookie');

import * as api from '../../components/api/index.js';
import url from '../../components/util/url.js';
import util from '../../components/util/util.js';
let Layer = require('../../libs/layer');

let pageEntry = (function() {
	return {
		initCheck() {
			let nowUrl = window.location.href;
			util.initLogin(nowUrl,"2");
		},
		setWindowHeight() {
			// 获取视窗高度，将背景图铺满屏幕
			let wh = (window.innerHeight > 0) ? window.innerHeight : screen.height;
			$('body').height(wh);
		},
		bindEvent() {
			let userCode = $.cookie("userCode");
			if(!(util.isEmpty(userCode))){
				api.indiviInfo(userCode).then(resp=>{
					if(resp.ret == "0"){
						// window.location = "/.html?registerType=1&agencyNumber="+resp.ret;
						let UserIsApply = resp.content.signFlag;//判断能不能报名，0-不能，1-能
						let userInfo = resp.content.userInfo;//用户信息
						let studentInfo = resp.content.studentInfo;//报名信息
						let questions = resp.content.questions;//考试大纲
						let examRecords = resp.content.examRecords;//考试记录
						// let registerTime = moment(userInfo.registerTime).format('M月DD日 h:mm');
						if(!(util.isEmpty(userInfo)) && !($.isEmptyObject(userInfo))){
							let registerTime = moment(userInfo.registerTime).format('YYYY.MM.DD');
							$(".registerTime").html(registerTime);
							$(".boundTel").html(userInfo.cellphone);
							$(".accountCode").html(userInfo.idCode);
							 // if(userInfo.status == "0"){//当前用户未报名
									// 	UserExamStatus = "0";
									// }
							// if(userInfo.status == "0"){//审核中

							// }else if(userInfo.status == "1"){//审核通过
									
							// }
							// 
						}else{
							$(".userInfo-form").hide();
						}
						if(!util.isEmpty(examRecords) && examRecords.length>0){
							let recordsHtml = "";
							for(let item in examRecords){
								let examRecord = examRecords[item];
								let appleTime = moment(examRecord.createTime).format('YYYY.MM.DD');
								let examTime = moment(examRecord.updateTime).format('YYYY.MM.DD');
								recordsHtml += "<div class='page-apply-form-panel indivi-register-content indivi-margin-top'><div class='register-content invidi-info'>"
									+ "<div class='content-left'>报名时间：</div><div class='content-right signTime'>" +appleTime+ "</div></div>"
									+ "<div class='register-content invidi-info'><div class='content-left'>考试时间：</div><div class='content-right signTime'>" +examTime+ "</div></div>"
									+ "<div class='register-content invidi-info'><div class='content-left'>考生姓名：</div><div class='content-right examName'>"
									+ examRecord.studentName + "</div></div><div class='register-content invidi-info'><div class='content-left'>考生身份证：</div>"
									+ "<div class='content-right examIdcard'>" + examRecord.studentIdcard + "</div></div><div class='register-content invidi-info'>"
									+ "<div class='content-left'>考试级别：</div><div class='content-right examType'>" + examRecord.levelCode + "</div></div>"
									+ "<div class='register-content invidi-info'><div class='content-left'>准考证号：</div><input class='content-right examCode' value=" +examRecord.examCode+ " readonly>"
									+"</div></div>";
									// if(examRecord.xxx){//当前考试结束
									// 	UserExamStatus = "1";
									// }
							}
							$(".records-hide").after(recordsHtml);
							$(".records-hide").show();
						}
						if(!($.isEmptyObject(studentInfo))){
							let examStatus = studentInfo.examStatus;//考试状态
							let signTime = moment(studentInfo.signTime).format('YYYY.MM.DD');
							let examCodeStatus = studentInfo.examCode;
							if(studentInfo.status == "0"){
								examCodeStatus = "审核中";
							}
							let studentHtml = "<div class='page-apply-form-panel indivi-register-content indivi-margin-top'><div class='register-content invidi-info'>"
									+ "<div class='content-left'>报名时间：</div><div class='content-right signTime'>" +signTime+ "</div></div>"
									+ "<div class='register-content invidi-info'><div class='content-left'>考生姓名：</div><div class='content-right examName'>"
									+ studentInfo.studentName + "</div></div><div class='register-content invidi-info'><div class='content-left'>考生身份证：</div>"
									+ "<div class='content-right examIdcard'>" + studentInfo.studentIdcard + "</div></div><div class='register-content invidi-info'>"
									+ "<div class='content-left'>考试级别：</div><div class='content-right examType'>" + studentInfo.levelCode + "</div></div>"
									+ "<div class='register-content invidi-info'><div class='content-left'>准考证号：</div><input class='content-right examCode' value=" +examCodeStatus+ " readonly>"
									+"</div></div>";
							$(".apply-hide").after(studentHtml);
							$(".apply-hide").show();
							if(examStatus = "0"){//未考试
								$("#startApply").hide();
								$("#startExam").show();
								// if(examCodeStatus == "审核中"){
								// 	$(".hide").hide();
								// }else{
								// 	$(".hide").show();
								// }
							}else if(examStatus == "1"){//考试完毕
								$("#startExam").hide();
								$("#startApply").show();
								$(".hide").hide();
							}
						}else{//如果报名信息为空，则报名
							$("#startApply").show();
							$("#startApply").off("click").on("click",function(){
								if(UserIsApply == "1"){
									location.href = "/indiviEnrol.html";
								}else{
									Layer.msg('考级评测中，</br>请等待审核结果');
									return;
								}
							});
						}
						if(!(util.isEmpty(questions)) && questions.length>0){
							// let questionsTitle = ['自我介绍1','朗诵2','才艺表演3','游戏4','亲子互动5','活动感悟6','结束7'];
							let indiviHtml = "";
							for(let item in questions){
								let questionsTitle = util.initExamTitle(parseInt(questions[item].questionType)-1);
								indiviHtml += "<div class='register-content invidi-info'><div class='content-left'>第" 
										+ util.intToCapital(item) + "题：</div><div class='content-right'>"+questionsTitle
										+ "</div></div><div class='register-content invidi-info'><div class='content-left questions-content'><pre>"
										+ questions[item].questionOutline+"</pre></div></div>"
							}
							$("#examOutline").empty().html(indiviHtml);
							$(".hide").show();
							$("#startExam").show();
							$("#startExam").off("click").on("click",function(){
								let studentName = studentInfo.studentName;//考生姓名
								let examCode = studentInfo.examCode;//准考证号
								localStorage.setItem('userName', studentName);
								api.getExamInfo($.cookie('userCode'), studentName, examCode).then((resp) => {
									let examType = 0;

									if (resp.ret == '0') {
										// 获得已完成题数
										let currentExam = resp.content.paper.examQuestionIndex;
										if(currentExam == 7){
											Layer.msg('您已经考试完毕</br>请等待审核结果');
											return;
										}
										// 获得应该进入题目的模板
										examType = resp.content.paper.questions[currentExam].answerWay;

										if (examType == 1) {//音频
											window.location.href = '/examAudio.html?examCode=' + examCode;
										}
										else if (examType == 2) {//视频
											window.location.href = '/examVideo.html?examCode=' + examCode;
										}
									}
								});
							});
						}else{
							$(".hide").hide();
							$("#startExam").off("click").on("click",function(){
								if(studentInfo.examStatus == "0"){
									Layer.msg('您已经报名完毕</br>请等待审核结果');
									return;
								}
							});
						}
						// if(!($.isEmptyObject(studentInfo)) && !(util.isEmpty(questions)) && questions.length>0){
						// 	$(".hide").show();
						// }
					}else{
						Layer.msg('获取信息失败</br>请重新登录');
			            setTimeout(function(){
			                window.location.href = "/indiviLogin.html";
			            },2000);
					}
				});
				$("#exitLogin").off("click").on("click",function(){
					util.clearAllCookie();
					location.href = "/indiviLogin.html";
				});
			}
		}
	}
})();

pageEntry.initCheck();
pageEntry.setWindowHeight();
pageEntry.bindEvent();
